package org.behrang.macbeans.tab.coda;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.plaf.ComponentUI;
import org.behrang.macbeans.Utils;
import org.netbeans.swing.tabcontrol.TabDisplayer;
import org.netbeans.swing.tabcontrol.plaf.AquaEditorTabDisplayerUI;
import org.netbeans.swing.tabcontrol.plaf.TabCellRenderer;
import org.netbeans.swing.tabcontrol.plaf.TabControlButton;
import org.netbeans.swing.tabcontrol.plaf.TabControlButtonFactory;

/**
 *
 * @author Behrang Saeedzadeh <behrangsa@gmail.com>
 */
public class CodaEditorTabDisplayerUI extends AquaEditorTabDisplayerUI {

    public static final int PREFERRED_HEIGHT = 22;
    public static final int MINIMUM_X_POSITION = 0;
    private JPanel controlPanel;
    private TabControlButton dropDownButton;
    private static Map<Integer, String[]> buttonIconPaths;

    public static ComponentUI createUI(JComponent c) {
        return new CodaEditorTabDisplayerUI((TabDisplayer) c);
    }

    public CodaEditorTabDisplayerUI(TabDisplayer displayer) {
        super(displayer);
    }

    @Override
    public void install() {
        super.install();
        scroll().setMinimumXposition(MINIMUM_X_POSITION);
    }

    @Override
    protected TabCellRenderer createDefaultRenderer() {
        return new CodaEditorTabCellRenderer();
    }

    @Override
    public Dimension getPreferredSize(JComponent c) {
        Dimension ps = super.getPreferredSize(c);
        ps.height = PREFERRED_HEIGHT;
        return ps;
    }

    @Override
    public Rectangle getTabRect(int tabIndex, Rectangle bounds) {
        Rectangle tabRect = super.getTabRect(tabIndex, bounds);
        tabRect.height = PREFERRED_HEIGHT - 2;
        return tabRect;
    }

    @Override
    protected Font createFont() {
        Font f = super.createFont();
        return f;
    // return f.deriveFont(11f);
    }
    public static Color UL = new Color(64, 64, 64);
    public static Color BL = new Color(170, 170, 170);
    public static Color BG_TOP = new Color(237, 237, 237);
    public static Color BG_BOTTOM = new Color(210, 210, 210);

    @Override
    protected void paintAfterTabs(Graphics gr) {
        Graphics2D g = (Graphics2D) gr;

        int rightLineStart = getTabsAreaWidth() - MINIMUM_X_POSITION - 1;
        int rightLineEnd = displayer.getWidth();

        if (displayer.getModel().size() > 0 && !scroll().isLastTabClipped()) {
            int idx = scroll().getLastVisibleTab(displayer.getWidth());
            rightLineStart = scroll().getX(idx) + scroll().getW(idx);
        } else if (displayer.getModel().size() == 0) {
            rightLineStart = 6;
        }

        int y = displayer.getY() + 1;
        int h = displayer.getHeight() - 1;

        // background
        GradientPaint gp =
                new GradientPaint(0, 1, BG_TOP, 0, h - 1, BG_BOTTOM);
        g.setPaint(gp);
        Rectangle2D.Double bg =
                new Rectangle2D.Double(rightLineStart, 1, rightLineEnd - rightLineStart, h - 3);
        g.fill(bg);

        // upper line
        g.setColor(UL);
        g.drawLine(rightLineStart, 0, rightLineEnd, 0);

        g.setColor(BL);

        // left line
        g.drawLine(rightLineStart, 1, rightLineStart, h - 2);

        // bottom line        
        g.drawLine(rightLineStart + 1, h - 2, rightLineEnd, h - 2);

        // right line
        g.drawLine(rightLineEnd - 1, 1, rightLineEnd - 1, h - 2);
    }

    @Override
    protected Component getControlButtons() {
        if (null == controlPanel) {
            JPanel buttonsPanel = new JPanel(null);
            buttonsPanel.setOpaque(false);

            int width = 0;
            int height = 0;

            dropDownButton = TabControlButtonFactory.createDropDownButton(displayer, false);
            buttonsPanel.add(dropDownButton);

            Dimension prefDim = dropDownButton.getPreferredSize();
            dropDownButton.setBounds(width, 0, 18, prefDim.height);

            width += 18;
            height = prefDim.height;

            Dimension size = new Dimension(width + 2, height);
            buttonsPanel.setMinimumSize(size);
            buttonsPanel.setSize(size);
            buttonsPanel.setPreferredSize(size);
            buttonsPanel.setMaximumSize(size);

            controlPanel = buttonsPanel;
        }

        return controlPanel;
    }

    @Override
    protected Rectangle getControlButtonsRectangle(Container parent) {
        Component c = getControlButtons();
        Rectangle rect =
                new Rectangle(parent.getWidth() - c.getWidth(),
                0,
                c.getWidth(),
                c.getHeight());

        return rect;
    }

    private static void initIcons() {
        if (null == buttonIconPaths) {
            buttonIconPaths = new HashMap<Integer, String[]>(1);

            //drop down button  
            String[] iconPaths = new String[4];
            iconPaths[TabControlButton.STATE_DEFAULT] = "resources/tab_menu.png"; // NOI18N
            iconPaths[TabControlButton.STATE_DISABLED] = "resources/tab_menu.png"; // NOI18N
            iconPaths[TabControlButton.STATE_ROLLOVER] = "resources/tab_menu.png"; // NOI18N
            iconPaths[TabControlButton.STATE_PRESSED] = "resources/tab_menu.png"; // NOI18N
            buttonIconPaths.put(TabControlButton.ID_DROP_DOWN_BUTTON, iconPaths);
        }
    }

    public Icon getButtonIcon(int buttonId, int buttonState) {
        Icon res = null;
        initIcons();
        String[] paths = buttonIconPaths.get(buttonId);
        if (null != paths && buttonState >= 0 && buttonState < paths.length) {            
            res = new ImageIcon(getClass().getResource(paths[0]));
        }
        return res;
    }
}
